package com.ruoyi.merchant.service.impl;

import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.merchant.domain.PmsCoupon;
import com.ruoyi.merchant.mapper.PmsCouponMapper;
import com.ruoyi.merchant.mapper.PmsMerchantCouponMapper;
import com.ruoyi.merchant.mapper.PmsSkuCouponMapper;
import com.ruoyi.merchant.service.PmsCouponService;
import com.ruoyi.system.common.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 28173
 * #Description PmsCouponServiceImpl
 * #Date: 2025/1/14 10:04
 */
@Service
public class PmsCouponServiceImpl implements PmsCouponService {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private PmsCouponMapper pmsCouponMapper;
    @Autowired
    private PmsMerchantCouponMapper pmsMerchantCouponMapper;
    @Autowired
    private PmsSkuCouponMapper pmsSkuCouponMapper;
    /**
     * 商家优惠券列表
     * @param pmsCoupon
     * @return
     */
    @Transactional
    @Override
    public List<PmsCoupon> couponList(PmsCoupon pmsCoupon) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long merchantId = pmsCouponMapper.GetPmsMerchantCouponByUserId(loginUser.getUserid());
        pmsCoupon.setMerchantId(merchantId);
        List<PmsCoupon> pmsCouponList = pmsCouponMapper.couponList(pmsCoupon);
        return pmsCouponList;
    }

    /**
     * 添加优惠券
     * @param pmsCoupon
     */
    @Transactional
    @Override
    public void couponAdd(PmsCoupon pmsCoupon) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long merchantId = pmsCouponMapper.GetPmsMerchantCouponByUserId(loginUser.getUserid());
        pmsMerchantCouponMapper.pmsMerchantCouponAdd(merchantId,pmsCoupon.getId());
        Integer couponId = pmsCouponMapper.couponAdd(pmsCoupon);
        pmsSkuCouponMapper.pmsSkuCouponAdd(couponId,pmsCoupon.getDesignatedGoods());
    }

    /**
     * 修改优惠券
     * @param pmsCoupon
     */
    @Override
    public void couponUpd(PmsCoupon pmsCoupon) {
        pmsCouponMapper.couponUpd(pmsCoupon);
    }
    /**
     * 删除优惠券
     * @param id
     */
    @Override
    public void couponDelById(Integer id) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long merchantId = pmsCouponMapper.GetPmsMerchantCouponByUserId(loginUser.getUserid());
        pmsMerchantCouponMapper.pmsMerchantCouponDelById(merchantId,id);
        pmsCouponMapper.couponDelById(id);
    }
    /**
     * 优惠券状态修改为失效
     * @param id
     */
    @Override
    public void updCouponStatusById(Integer id) {
        pmsCouponMapper.updCouponStatusById(id);
    }

    /**
     * 复制优惠券
     * @param pmsCoupon
     */
    @Override
    public void couponCopy(PmsCoupon pmsCoupon) {

    }
}
